package com.titleevaluation.service;

import com.titleevaluation.entity.Application;
import com.titleevaluation.entity.User;
import com.titleevaluation.repository.ApplicationRepository;
import com.titleevaluation.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class ApplicationService {

    private final ApplicationRepository applicationRepository;
    private final UserRepository userRepository;

    public ApplicationService(ApplicationRepository applicationRepository, UserRepository userRepository) {
        this.applicationRepository = applicationRepository;
        this.userRepository = userRepository;
    }

    public Application createApplication(Long userId) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("User not found"));
        Application application = new Application();
        application.setUser(user);
        application.setApplicationNo(UUID.randomUUID().toString()); // 生成唯一申报编号
        application.setStatus("draft");
        application.setCreatedAt(LocalDateTime.now());
        application.setUpdatedAt(LocalDateTime.now());
        return applicationRepository.save(application);
    }

    public Optional<Application> getApplicationById(Long id) {
        return applicationRepository.findById(id);
    }

    public List<Application> getApplicationsByUserId(Long userId) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("User not found"));
        return applicationRepository.findByUser(user);
    }

    public Application updateApplication(Long id, Application updatedApplication) {
        Application existingApplication = applicationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Application not found"));

        existingApplication.setApplyLevel(updatedApplication.getApplyLevel());
        existingApplication.setApplyProfession(updatedApplication.getApplyProfession());
        existingApplication.setApplyQualificationName(updatedApplication.getApplyQualificationName());
        existingApplication.setApplyType(updatedApplication.getApplyType());
        existingApplication.setIsRuralTalent(updatedApplication.getIsRuralTalent());
        existingApplication.setApplyCommittee(updatedApplication.getApplyCommittee());
        existingApplication.setApplyPointName(updatedApplication.getApplyPointName());
        existingApplication.setApplyPointAddress(updatedApplication.getApplyPointAddress());
        existingApplication.setApplyPointPhone(updatedApplication.getApplyPointPhone());
        existingApplication.setApplyPointEmail(updatedApplication.getApplyPointEmail());
        existingApplication.setIsHighLevelTalent(updatedApplication.getIsHighLevelTalent());
        existingApplication.setIsHighLevelTalentGeneral(updatedApplication.getIsHighLevelTalentGeneral());
        existingApplication.setIsPhdTalent(updatedApplication.getIsPhdTalent());
        existingApplication.setIsOverseasReturnee(updatedApplication.getIsOverseasReturnee());
        existingApplication.setUpdatedAt(LocalDateTime.now());

        return applicationRepository.save(existingApplication);
    }

    public void deleteApplication(Long id) {
        applicationRepository.deleteById(id);
    }

    public Application submitApplication(Long id) {
        Application application = applicationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Application not found"));
        application.setStatus("submitted");
        application.setSubmittedAt(LocalDateTime.now());
        return applicationRepository.save(application);
    }

    public Application updateApplicationStatus(Long id, String status) {
        Application application = applicationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Application not found"));
        application.setStatus(status);
        return applicationRepository.save(application);
    }
}

